package ar.com.android.restoapp.adapters;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ar.com.android.restoapp.forms.R;
import ar.com.android.restoapp.entities.OrderItems;

public class ListViewOrderAdapter extends ArrayAdapter<OrderItems>
{
	public List<OrderItems> items;
	Activity activity;
	
	public ListViewOrderAdapter(Activity activity, int textViewResourceId, List<OrderItems> items) {
		super(activity, textViewResourceId, items);
		this.items = items;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public OrderItems getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (items.get(position)).getOrderItemId();
	}

	private class ViewHolder {
		ImageView imgSelected;
		TextView orderQuantity;
	    TextView orderDescription;
	    TextView orderPrice;
	 }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		LayoutInflater inflater =  activity.getLayoutInflater();

		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.lv_order_item, null);
			holder = new ViewHolder();
			holder.imgSelected = (ImageView) convertView.findViewById(R.id.imgSelected);
			holder.orderQuantity = (TextView) convertView.findViewById(R.id.orderQuantity);
			holder.orderDescription = (TextView) convertView.findViewById(R.id.orderDescription);
			holder.orderPrice = (TextView) convertView.findViewById(R.id.orderPrice);

			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder) convertView.getTag();

		OrderItems orderItem = items.get(position);
		if(orderItem.getSelected())
			holder.imgSelected.setImageResource(R.drawable.ic_sel_item);
		else
			holder.imgSelected.setImageResource(R.drawable.ic_unsel_item);
		holder.orderQuantity.setText(orderItem.getOrderQuantity().toString());
		holder.orderDescription.setText(orderItem.getOrderDescription());
		holder.orderPrice.setText(orderItem.getPrice().toString());

		return convertView;	
	}

}
